package com.engineering.elink.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.engineering.elink.service.TokenAuthenticationService;
import com.engineering.elink.service.UserService;
import com.engineering.elink.util.data.Format;
import com.engineering.elink.util.global.ResponseStatus;
import com.engineering.elink.util.tool.JSONConvert;

@CrossOrigin
@Controller
@RequestMapping("/auth")
public class AuthTokenController {

	@Autowired
	private TokenAuthenticationService tokenAuthenticationService;
	@Autowired
	private	UserService userService;

	/**
	 * 认证身份
	 * @param username
	 * @return 0 身份认证失败 1身份认证成功
	 */
	@RequestMapping(value = "/checkAuthToken", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject checkAuthToken(@Param("seed") String seed,
			@Param("authToken") String authToken) {
		int code = ResponseStatus.AuthTokenExpired;
		String msg = ResponseStatus.AuthTokenExpiredMessage;
		String userId = tokenAuthenticationService.getAuthTokenInfo(seed,
				authToken);
		if (Format.checkSeed(seed) && Format.checkAuthToken(authToken)) {
			if (userId != null) {
				code = ResponseStatus.RequestSuccess;
				msg = ResponseStatus.RequestSuccessMessage;
			}
		}
		return JSONConvert.getJSON(null, code, msg);
	}
	
}
